//
//  UIControl+Block.m
//  YDGJ
//
//  Created by ydcq on 16/9/7.
//  Copyright © 2016年 Galaxy360. All rights reserved.
//

#import "UIControl+Block.h"
#import <objc/runtime.h>

static const void *blkKey = "controlBlock";

@implementation UIControl (Block)

- (void)addActionBlock:(void (^)(NSInteger))blk forControlEvents:(UIControlEvents)event
{
    objc_setAssociatedObject(self, blkKey, blk, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(action:) forControlEvents:event];
}

- (void)action:(id)sender
{
    void (^blk)(NSInteger) = objc_getAssociatedObject(self, blkKey);
    if (blk) {
        blk(self.tag);
    }
}

@end
